/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 5/20/18
Time: 8:35 PM
 */

package Cn.Sarkar.MMS.Entities.DataSources.Sources

import Cn.Sarkar.DataVersionManager
import Cn.Sarkar.Entities.Common.TableNoID
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.Core.Extensions.dbCollection
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.Core.IDBTable
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.MySql.MySqlDataContext
import Cn.Sarkar.EntityDream.Pipeline.Extension.installFeature
import Cn.Sarkar.MMS.DataBaseDefinations.Additional.*
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.*
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Permission.PermissionGroups
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Permission.PermissionItems
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Permission.Permissions
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Wallet.ChangeHistorys
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Wallet.MemberShipLevels
import Cn.Sarkar.MMS.DataBaseDefinations.Independent.Wallet.Wallets
import Cn.Sarkar.MMS.Entities.Additional.*
import Cn.Sarkar.MMS.Entities.DataSources.IDataSource
import Cn.Sarkar.MMS.Entities.Independent.*
import Cn.Sarkar.MMS.Entities.Independent.Permission.Permission
import Cn.Sarkar.MMS.Entities.Independent.Permission.PermissionGroup
import Cn.Sarkar.MMS.Entities.Independent.Permission.PermissionItem
import Cn.Sarkar.MMS.Entities.Independent.Wallet.ChangeHistory
import Cn.Sarkar.MMS.Entities.Independent.Wallet.MemberShipLevel
import Cn.Sarkar.MMS.Entities.Independent.Wallet.Wallet
import Cn.Sarkar.logger
import java.sql.Connection


class MySqlDataSource(connection: Connection) : MySqlDataContext(connection), IDataSource {
    override val tables: Array<IDBTable> = arrayOf(*IDataSource.AllTables)
//class MySqlDataSource : SQLiteDataContext(DriverManager.getConnection("jdbc:sqlite:SarkarMMS.db")) {

    override val contacts = dbCollection(Contacts) { Contact(it) }
    override val countries = dbCollection(Countries) { Country(it) }
    override val descriptions = dbCollection(Descriptions) { Description(it) }
    override val names = dbCollection(Names) { Name(it) }
    override val thirdPartyAccounts = dbCollection(ThirdPartyAccounts) { ThirdPartyAccount(it) }
    override val types = dbCollection(Types) { Type(it) }

    override val permissionGroups = dbCollection(PermissionGroups) { PermissionGroup(it) }
    override val permissionItems = dbCollection(PermissionItems) { PermissionItem(it) }
    override val permissions = dbCollection(Permissions) { Permission(it) }

    override val changeHistorys = dbCollection(ChangeHistorys) { ChangeHistory(it) }
    override val memberShipLevels = dbCollection(MemberShipLevels) { MemberShipLevel(it) }
    override val wallets = dbCollection(Wallets) { Wallet(it) }

    override val accounts = dbCollection(Accounts) { Account(it) }
    override val applications = dbCollection(Applications) { Application(it) }
    override val applicationDomains = dbCollection(ApplicationDomains) { ApplicationDomain(it) }
    override val companies = dbCollection(Companies) { Company(it) }
    override val connectApplicationAndAccountAndWallet = dbCollection(DomainRelations) { DomainRelation(it) }
    override val persons = dbCollection(Persons) { Person(it) }

    init {
        pipeLine.installFeature(logger())
        pipeLine.installFeature(DataVersionManager())

        checkTablesCategoryNumberError()
    }

    protected fun checkTablesCategoryNumberError() {
        tables.map { it as TableNoID }.groupBy { it.CATEGORY }.filter { it.value.size > 1 }.also {
            if (it.isNotEmpty()) {
                throw Exception("جەدۋەلىكى كاتىگورىيە نومۇرىدا ئوخشاشلىق بار، نومۇرنى ئۆزگەرتىڭ، ئوخشاشلىق بار جەدۋەللەر تۋەندىكىدەك: \n\n" +
                        it.map { it.value.first().TableName }.joinToString())
            }
        }
    }
}